package com.example.zqq.mysparkview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Date;


/**
 * Created by Administrator on 2017/11/2.
 */

public class SparkView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    public static int WIDTH, HEIGHT;           // 屏幕宽高

    private boolean isRun;
    private float X, Y;                // 当前触摸点X，Y坐标

    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    private SparkManager sparkManager;


    public SparkView(Context context) {
        super(context);
        setLayerType(LAYER_TYPE_SOFTWARE, null);        //关闭硬件加速
        DisplayMetrics metrics = new DisplayMetrics();   // 设置视图宽高（像素）
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        WIDTH = metrics.widthPixels;
        HEIGHT = metrics.heightPixels;

        // 火花管理器
        sparkManager = new SparkManager();

        mHolder = this.getHolder();
        mHolder.addCallback(this);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getPointerCount()) {
            case 1:      // 单点触摸
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        sparkManager.isActive = true;
                        X = event.getX();
                        Y = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        sparkManager.isActive = false;
                        break;
                }
                break;
        }
        return true;
    }

    //在创建时激发，一般在这里调用画图的线程。
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isRun = true;
        new Thread(this).start();
    }

    //在surface的大小发生改变时激发
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        drawBackgound();
    }


    //销毁时激发，一般在这里将画图的线程停止、释放。
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isRun = false;
    }


    @Override
    public void run() {
        // 火花数组
        int[][] sparks = new int[400][10];
        Date date;
        while (isRun) {
            date = new Date();

            try {
                mCanvas = mHolder.lockCanvas(null);

                if (mCanvas != null) {

                    synchronized (mHolder) {
                        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);    // 清屏

                        // 循环绘制所有火花
                        for (int[] n : sparks) {
                            n = sparkManager.drawSpark(mCanvas, (int) X, (int) Y, n);
                        }
                        Thread.sleep(Math.max(0, 30 - (new Date().getTime() - date.getTime())));
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (mCanvas != null) {
                    mHolder.unlockCanvasAndPost(mCanvas);
                }
            }
        }
    }

    private void drawBackgound() {
        mCanvas = mHolder.lockCanvas();
        mCanvas.drawColor(Color.WHITE);
        mHolder.unlockCanvasAndPost(mCanvas);
    }

}
